<template>
  <div>
    <!-- 面包屑 -->
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>工人管理</el-breadcrumb-item>
      <el-breadcrumb-item>工人详情</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card class="box-card">
      <template>
        <el-tabs v-model="activeName" type="card" @tab-click="handleClick">
          <el-tab-pane label="工人信息" name="worker_info">
            <template>
              <el-form :model="workerForm" ref="workerForm" label-width="100px" class="demo-ruleForm">
                <el-form-item label="姓名" required style="width: 600px;" prop="name">
                  <el-input v-model="workerForm.name"></el-input>
                </el-form-item>
                <el-form-item label="手机号" required style="width: 600px;" prop="mobile">
                  <el-input v-model="workerForm.mobile"></el-input>
                </el-form-item>
                <el-form-item label="年龄" required style="width: 600px;" prop="age">
                  <el-input type="number" v-model.number="workerForm.age"></el-input>
                </el-form-item>
                <el-form-item label="案例数" required style="width: 600px;" prop="case_count">
                  <el-input type="number" v-model.number="workerForm.case_count"></el-input>
                </el-form-item>
                <el-form-item label="工龄" required style="width: 600px;" prop="work_age">
                  <el-input type="number" v-model.number="workerForm.work_age"></el-input>
                </el-form-item>
                <el-form-item label="身份证号" required style="width: 600px;" prop="id_number">
                  <el-input v-model="workerForm.id_number"></el-input>
                </el-form-item>
                <el-form-item label="技能" required prop="skills" style="width: 600px;">
                  <el-input v-model="workerForm.skills"></el-input>
                </el-form-item>
                <el-form-item label="评分" required style="width: 600px;" prop="start">
                  <el-input type="number" v-model.number="workerForm.start"></el-input>
                </el-form-item>
                <el-form-item label="头像" required prop="face">
                  <el-upload class="upload-demo" accept="image/*" :headers="uploadParams.uploadHeaders" :data="uploadParams.uploadData"
                    :action="uploadParams.uploadFileUrl" list-type="picture" :limit="1" :on-exceed="handleExceed"
                    :on-success="uploadSuccess">
                    <el-button size="small" type="primary">点击上传</el-button>
                  </el-upload>
                </el-form-item>
                <div v-if="workerForm.face_path != ''" style="height: 150px;width: 300px;margin-left: 100px;margin-bottom: 20px;">
                  <el-image style="height: 100%;" :src="workerForm.face_path" fit="fill"></el-image>
                </div>
                <el-form-item>
                  <el-button type="primary" @click="updateOrCreateWorkerForm">保存</el-button>
                  <el-button v-if="operate == 'add'" @click="resetForm">重置</el-button>
                </el-form-item>
              </el-form>
            </template>
          </el-tab-pane>
        </el-tabs>
      </template>
    </el-card>
  </div>
</template>

<script>
  import {
    _updateOrCreateWorkerApi,
    _getWorkerApi,
    getUploadParamsApi
  } from '../request/api.js'
  export default {
    data() {
      return {
        operate: 'add',
        workerId: '',
        workerForm: {
          name: '',
          mobile: '',
          id_number: '',
          age: '',
          skills: '',
          work_age: '',
          case_count: '',
          face_path: '',
          face: 1,
          start: 5
        },
        // 文件数据
        uploadParams: {},
        // 页面样式
        activeName: 'worker_info'
      }
    },
    created() {
      // 获取文件上传的参数配置
      this.uploadParams = getUploadParamsApi('worker')
      // 判断是create or update
      const workerId = this.$route.query.workerId
      if (workerId !== undefined && workerId !== 'add') {
        this.operate = 'edit'
        this.getWorker(workerId)
      }
    },
    methods: {
      async getWorker(workerId) {
        const {
          data: res
        } = await _getWorkerApi(workerId)
        console.log(res.data)
        this.workerForm = res.data
      },
      updateOrCreateWorkerForm() {
        this.$refs.workerForm.validate(async valid => {
          console.log(valid)
          if (valid) {
            const {
              data: res
            } = await _updateOrCreateWorkerApi(this.workerForm, this.operate)
            console.log(res)
            if (res.status) {
              var message = this.operate === 'add' ? '添加成功！' : '编辑成功！'
              this.$message.success(message)
              // 跳转到编辑页
              this.$router.push({
                name: 'worker'
              })
            } else {
              this.$message.error('操作失败！')
            }
          }
        })
      },
      // 重置表单
      resetForm() {
        this.$refs.workerForm.resetFields()
      },
      // 文件上传
      uploadSuccess(response) {
        console.log(response)
        if (response.status) {
          // 保存id 和显示的图片地址
          this.workerForm.face = response.data.id
        }
      },
      handleExceed(files, fileList) {
        this.$message.warning('当前限制上传 1 个文件')
      },
      // 标签页事件
      handleClick(tab, event) {
        this.activeName = tab.name
      }
    }
  }
</script>

<style lang="less" scoped>
</style>
